package com.shk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shk.annotation.Log4jAnnotation;
import com.shk.constant.MessageConstant;
import com.shk.entity.PageResult;
import com.shk.entity.QueryPageBean;
import com.shk.entity.Result;
import com.shk.pojo.Permission;
import com.shk.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("/add")
    @Log4jAnnotation
    public Result add(@RequestBody Permission permission) throws  Exception{
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findPage")
    @Log4jAnnotation
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult page = permissionService.findPage(currentPage, pageSize, queryString);
        return page;
    }

    @RequestMapping("/findById")
    @Log4jAnnotation
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
    }


    @RequestMapping("/edit")
    @Log4jAnnotation
    public Result update(@RequestBody Permission permission){
            permissionService.update(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);

    }
    @RequestMapping("/delete")
    @Log4jAnnotation
    public Result delete(Integer id){
        permissionService.delete(id);
        return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    @RequestMapping("/findAll")
    @Log4jAnnotation
    public Result findAll(){
        List<Permission> data = permissionService.findAll();
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,data);
    }

}
